package org.udp;

import java.io.IOException;
import java.net.SocketException;
import java.util.HashMap;
import java.util.Map;

/**
 * 基于udp的一个字典服务
 */
public class UDPDictServer extends UDPEchoServer{
    //定义一个字典Map
    private Map<String, String> map;
    public UDPDictServer(int port) throws SocketException {
        super(port);
        //初始化字典内容
        map = new HashMap<>();
        map.put("tiger", "老虎");
        map.put("cat", "猫");
        map.put("dog", "狗");
        map.put("pig", "猪");
    }

    @Override
    protected String process(String key) {
        String response = map.getOrDefault(key, "查无此词");
        return response;
    }

    public static void main(String[] args) throws IOException {
        UDPDictServer server = new UDPDictServer(8888);
        server.start();
    }
}
